home *** CD-ROM | disk | FTP | other *** search
/ Personal Computer World 2009 February / PCWFEB09.iso / Software / Resources / Chat & Communication / Digsby build 37 / digsby_setup.exe / lib / common / asynchttp / requester.pyo (.txt) < prev   
Python Compiled Bytecode  |  2008-10-13  |  5KB  |  149 lines

  1. # Source Generated with Decompyle++
  2. # File: in.pyo (Python 2.5)
  3.  
  4. import atexit
  5. import logging
  6. import httplib
  7. import util
  8. _log = log = logging.getLogger('asynchttp')
  9. import connection
  10. import httptypes
  11. __all__ = [
  12.     'HttpMaster',
  13.     'httpopen']
  14.  
  15. class HttpMaster(object):
  16.     
  17.     def key(thing):
  18.         if isinstance(thing, (connection.HttpPersister, connection.AsyncHttpConnection)):
  19.             return '%s:%s' % (thing.host, thing.port)
  20.         elif isinstance(thing, httptypes.HTTPRequest):
  21.             host = thing.get_host()
  22.             o_r_host = thing.get_origin_req_host()
  23.             if o_r_host in host:
  24.                 if o_r_host != host:
  25.                     pass
  26.                 else:
  27.                     host = o_r_host
  28.             
  29.             url = thing.get_full_url()
  30.             if ':' in host:
  31.                 (host, port) = host.split(':')
  32.             elif url.startswith('https'):
  33.                 port = httplib.HTTPS_PORT
  34.             else:
  35.                 port = httplib.HTTP_PORT
  36.             return '%s:%s' % (host, port)
  37.         else:
  38.             raise TypeError("Don't know how to key this object: %r", thing)
  39.  
  40.     key = staticmethod(key)
  41.     
  42.     def __init__(self):
  43.         self._conns = { }
  44.  
  45.     
  46.     def __repr__(self):
  47.         return '<%s with %r active connections (id=0x%x)>' % (type(self).__name__, len(self._conns), id(self))
  48.  
  49.     
  50.     def request(self, full_url, data = None, *a, **k):
  51.         cb = k.pop('callback')
  52.         req = httptypes.HTTPRequest.make_request(full_url, data, *a, **k)
  53.         self._do_request(req, cb)
  54.  
  55.     request = util.callsback(request)
  56.     
  57.     def _do_request(self, req, cb = None):
  58.         if cb is None:
  59.             cb = req.callback
  60.         
  61.         conn = self.get_connection(req)
  62.         conn.request(req, callback = cb)
  63.  
  64.     
  65.     def get_connection(self, forwhat):
  66.         key = self.key(forwhat)
  67.         
  68.         try:
  69.             conn = self._conns[key]
  70.         except KeyError:
  71.             conn = self._conns[key] = self._make_connection(key)
  72.  
  73.         return conn
  74.  
  75.     
  76.     def _make_connection(self, key):
  77.         log.info('Making new %r for key=%r', connection.HttpPersister, key)
  78.         (host, s_port) = key.split(':')
  79.         port = int(s_port)
  80.         conn = connection.HttpPersister((host, port))
  81.         self.bind_events(conn)
  82.         return conn
  83.  
  84.     
  85.     def bind_events(self, conn):
  86.         bind = conn.bind_event
  87.         bind('on_fail', self._failed_connection)
  88.         bind('redirect', self._handle_redirect)
  89.  
  90.     
  91.     def unbind_events(self, conn):
  92.         unbind = conn.unbind
  93.         unbind('on_fail', self._failed_connection)
  94.         unbind('redirect', self._handle_redirect)
  95.  
  96.     
  97.     def _handle_redirect(self, req):
  98.         redirector = getattr(req, 'on_redirect', None)
  99.         if redirector is not None:
  100.             newreq = redirector(req)
  101.             if newreq is None:
  102.                 req.callback.error('redirect cancelled')
  103.                 return None
  104.             
  105.             req = newreq
  106.         
  107.         self._do_request(req)
  108.  
  109.     
  110.     def _failed_connection(self, conn):
  111.         log.info('Removing failed connection: conn = %r, key(conn) = %r', conn, self.key(conn))
  112.         self._conns.pop(self.key(conn))
  113.         self.unbind_events(conn)
  114.  
  115.     
  116.     def close_all(self):
  117.         while self._conns:
  118.             (_key, conn) = self._conns.popitem()
  119.             self.unbind_events(conn)
  120.             conn.close()
  121.  
  122.  
  123. _httpmaster = HttpMaster()
  124. atexit.register(_httpmaster.close_all)
  125.  
  126. def httpopen(*a, **k):
  127.     cb = k.pop('callback')
  128.     _httpmaster.request(callback = cb, *a, **k)
  129.  
  130. httpopen = util.callsback(httpopen)
  131.  
  132. def main():
  133.     
  134.     def success(*a):
  135.         print 'success', a
  136.  
  137.     
  138.     def error(*a):
  139.         print 'error', a
  140.  
  141.     httpopen('http://65.54.239.211/index.html', success = success, error = error)
  142.  
  143. if __name__ == '__main__':
  144.     from tests.testapp import testapp
  145.     a = testapp()
  146.     main()
  147.     a.MainLoop()
  148.  
  149.